﻿// Write a program that reads a number N and calculates the sum of the first N members of the sequence of 
// Fibonacci: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, …
// Each member of the Fibonacci sequence (except the first two) is a sum of the previous two members.

using System;
using System.Numerics;

class SumOfNFibonacciNumbers
{
    static void Main()
    {
        Console.Write("Enter number N bigger than 0 : ");
        string numberNStr = Console.ReadLine();
        uint numberN;
        while (!uint.TryParse(numberNStr, out numberN))
        {
            Console.Write("Invalid input! N should be bigger than 0. Please, enter N again : ");
            numberNStr = Console.ReadLine();
        }
        if (numberN == 0)
        {
            Console.WriteLine("N had to be bigger than 0, not equal. Start the program again!");
        }
        else if (numberN == 1)
        {
            Console.WriteLine("The value of N is 1, so the result is 0");
        }
        else
        {
            BigInteger firstNumber = 0;
            BigInteger secondNumber = 1;
            BigInteger nextNumber = 0;
            BigInteger sumFibonacci = 1;

            for (int i = 0; i < numberN - 2; i++)
            {
                nextNumber = firstNumber + secondNumber;
                firstNumber = secondNumber;
                secondNumber = nextNumber;
                sumFibonacci += nextNumber;
            }

            Console.WriteLine("The sum of the first {0} numbers of Fibonacci sequence is : {1}", numberN, sumFibonacci);
        }
    }
}
